#include "polhemux_get.h"
#include "usart.h"

extern UART_HandleTypeDef huart2;;
extern DMA_HandleTypeDef hdma_usart2_rx;
uint8_t huart2_rev_data[SensorOne_DataSize]; //一个传感器对应60的预留数据量
uint8_t polhemux_data_buff[SensorOne_DataSize];
uint8_t polhemux_get_xyz_command = 0x50;
#define polhemux_Hemispherical_tracking_command_len 9
#define polhemux_set_unit_command_len 3
uint8_t polhemux_Hemispherical_tracking_command[polhemux_Hemispherical_tracking_command_len] = {0x48, 0x2A, 0x2C, 0x30, 0x2C, 0x30, 0x2C, 0x30, 0x0D};
uint8_t polhemux_set_unit_command[polhemux_set_unit_command_len] = {0x55, 0x31, 0x0D};
int16_t rev_polhemux_data_length = 0;
uint64_t polehmeux_idlecount = 0;
uint16_t polhemux_rev_len = 0;
#define polhemux_sensor1_id 0x04
#define polhemux_use 1

void Polhemux_data_deal();
void polhemux_data_get(void);


void polhemux_init(void)
{
    #if polhemux_use
    // for(uint8_t i = 0; i < 3; i ++)
    // {
    //   HAL_UART_Transmit_IT(&huart2, polhemux_Hemispherical_tracking_command, polhemux_Hemispherical_tracking_command_len);
    //   HAL_Delay(10);
    // }

    // for(uint8_t i = 0; i < 3; i ++)
    // {
    //   HAL_UART_Transmit_IT(&huart2, polhemux_set_unit_command, polhemux_set_unit_command_len);
    //   HAL_Delay(5);
    // }

    // HAL_UART_Receive_IT(&huart2, huart2_rev_data, sizeof(huart2_rev_data));
    // HAL_UART_Transmit_IT(&huart2, &polhemux_get_xyz_command, 1);


    __HAL_UART_ENABLE_IT(&huart2, UART_FLAG_IDLE);
    HAL_UART_Receive_DMA(&huart2, huart2_rev_data, sizeof(huart2_rev_data));

    // __HAL_UART_ENABLE_IT(&huart2, UART_IT_TC);
    HAL_UART_Transmit_DMA(&huart2, &polhemux_get_xyz_command, 1);


    #endif
}


void Polhemux_data_deal(void)
{
  if(huart2_rev_data[0] == 0x30 && huart2_rev_data[1] == 0x34 && huart2_rev_data[SensorOne_DataSize - 2] == 0x0d && huart2_rev_data[SensorOne_DataSize - 1] == 0x0a)
  {
    // printf("tesing\n");
    // HAL_UART_Transmit_DMA(&huart2, huart2_rev_data, SensorOne_DataSize); //用DMA一定要开中断 否则无论如何只会触发一次 
    memcpy(polhemux_data_buff, huart2_rev_data, SensorOne_DataSize);
  }
}

void polhemux_data_get(void)
{
    #if polhemux_use
      // HAL_UART_Receive_IT(&huart2, huart2_rev_data, sizeof(huart2_rev_data));
      // HAL_UART_Transmit_IT(&huart2, &polhemux_get_xyz_command, 1);
      // osDelay(100);
      // Polhemux_data_deal();
      HAL_UART_Transmit_DMA(&huart2, &polhemux_get_xyz_command, 1);
    #endif
    
}

void rev_polhemux_data_analysis(void)
{
    int32_t read_temp = 0;
    uint16_t rev_data_length_temp = 0;
    // uint32_t ulReturn;
    if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE) != RESET)
    {
        // ulReturn = taskENTER_CRITICAL_FROM_ISR();
        // printf("IDLE...\n");
        polehmeux_idlecount ++;
        __HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_IDLE);
        HAL_UART_DMAStop(&huart2); 
        read_temp = (&huart2)->Instance->SR;
        read_temp = (&huart2)->Instance->DR;
        rev_data_length_temp = sizeof(huart2_rev_data) - (&hdma_usart2_rx)->Instance->NDTR;
        polhemux_rev_len = rev_data_length_temp;
        Polhemux_data_deal();
        memset(huart2_rev_data, sizeof(huart2_rev_data), 0);
        HAL_UART_Receive_DMA(&huart2, huart2_rev_data, sizeof(huart2_rev_data));
        // taskEXIT_CRITICAL_FROM_ISR( ulReturn );
    }
}

// void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
// {
//     if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TC))
//     {
//         __HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_TC);
//         osEventFlagsSet(Tx_Slave_eventHandle, tx_slave_finish_event);
//     }
// }




